<?php

class ModulesController extends ModulesAppController {
	
	var $name = 'Modules';
	//var $uses = array();
	var $helpers = array('Html','Form', 'Javascript', 'Cms.Tabs', 'Cms.Index','Modules.Section', 'Cms.Ajax');
	var $modules = array();
	var $components = array('RequestHandler');
	
	
	//cater ajax request only
	function admin_updatePosition()
	{
		$default = array('orders'=>null,'section'=>null);	
		$params = array_intersect_key($_POST,$default);
		$params = am($default,$params);
		$params['orders'] = explode(',',$params['orders']);
		$this->Module->updateSection($params);
		$this->autoRender = false;
	}
	function admin_delete($id)
	{
		$moduleData = $this->Module->findById($id);
		if(empty($moduleData))
		{
			if($this->RequestHandler->isAjax() == false)
			{
				$this->errorMsg(__('Invalid URL!'));
				$this->redirect(array('controller'=>'modules','action'=>'admin_index'));
			}else
			{
				$this->_stop();
			}
		}
		if($this->Module->del($id))
		{
			$fn = "successMsg";
			$msg = __("Module deleted!");
		}else
		{
			$fn = "errorMsg";
			$msg = __("Sorry, module could not be deleted!");
		}
		if($this->RequestHandler->isAjax() == false)
		{
				
			$this->$fn($msg);
			$this->redirect(array('controller'=>'modules','action'=>'admin_index'));
		}else
		{
			$this->autoRender = false;
		}
	}
	
	function admin_index()
	{
		$this->pageTitle = __('Modules');
		$this->paginate['limit'] = 20;
		$this->paginate['order'] = array('Module.id DESC');
		$modules = $this->paginate();
		$this->set('modules', $modules);		
	}
	
	function admin_edit($id)
	{
		$this->pageTitle = __("Edit Module");
		$viewFile = 'admin_edit';
		$moduleData = $this->Module->findById($id);
		if(!$moduleData)
		{
			if($this->RequestHandler->isAjax())
			{
				$this->_stop();
			}else
			{
				$this->redirect($this->referer());
			}
		}
		$this->set('module',$moduleData);
		// populate module params
		$this->Module->setModule($moduleData['Module']['name']); // for setting up params
		if($this->RequestHandler->isAjax())
		{
			Configure::write('Modules.moduleEditorMode',true);
			$this->set('changeModuleType',false);
			$viewFile = 'admin_ajax_edit';
		}
		if(!empty($this->data))
		{
			$this->data['Module']['id'] = $id;
			if($this->_save($this->data))
			{
				if($this->RequestHandler->isAjax() == false)
				{
					$this->successMsg(__('Module edited.'));
					$this->redirect('/admin/modules');
				}else
				{
					$moduleData = $this->Module->findById($id);
					$this->set('module',$moduleData);
				}
			}
			else
			{
				$this->set('savedFail',true);
				if($this->RequestHandler->isAjax() == false)
				{
					$this->errorMsg(__('Module can not be edited'));
				}
			}			
		}
		// populate module params
		if(!$this->data)
		{
			$this->data = $moduleData;
			if($this->data['Module']['params'])
			{
				$keys = array_keys($this->data['Module']['params']);
				$values = array_values($this->data['Module']['params']);
			
				$formattedParams = array_combine($keys,$values);
				$this->data['Module'] = am($this->data['Module'],$formattedParams);
			}
			
		}
		$isVisibilityFilterEnabled = false;
		if(!empty($this->data['ModuleVisibility']))
		{
			$isVisibilityFilterEnabled = true;
		}
		$this->set('isVisibilityFilterEnabled',$isVisibilityFilterEnabled);
		$this->render($viewFile);
	}
	
	function admin_add($sectionId = null)
	{
		$this->pageTitle = __('Add Module');
		$viewFile = 'admin_edit';
		if($this->RequestHandler->isAjax())
		{
			Configure::write('Modules.moduleEditorMode',true);
			$sections = $this->Module->getFieldProperty('section','options');
			$sections = array_keys($sections);
			if(in_array($sectionId,$sections) == false)
			{
				$this->_stop();
			}
			if($this->data)
			{
				$this->data['Module']['section']= $sectionId;
			}
			$this->set('changeModuleType',true);
			$viewFile = 'admin_ajax_edit';
		}
		
		if(!empty($this->data))
		{
			if($this->_save($this->data))
			{
				if($this->RequestHandler->isAjax() == false)
				{
					$this->successMsg(__('Module added'));
					$this->redirect('/admin/modules');
				}else
				{
					$id = $this->Module->id;
					$module = $this->Module->findById($id);
					$this->set('module',$module);
				}
			}
			else
			{
				$this->set('savedFail',true);
				$this->errorMsg(__('Module can not be added'));
			}			
			
		}
		if($this->RequestHandler->isAjax())
		{
			$this->data['Module']['section']= $sectionId;
		}
		$this->set('isVisibilityFilterEnabled',false);
		
		if(empty($this->data))
		{
			$this->Module->setFieldProperties('show_title',array('default'=>1));
			$this->Module->setFieldProperties('apply_default_class',array('checked'=>1));
		}
		$this->render($viewFile);		
	}
	
	function _formatDataToSave()
	{
		$this->Module->setModule($this->data['Module']['name']); // for setting up params
		//formatting params to save.	
		$paramsFields = ClassRegistry::getObject('Module')->getFields('group','params');
		if(!empty($paramsFields) && is_array($paramsFields))
		{
			foreach($paramsFields as $field)
			{
				$this->data['Module']['params'][$field] = $this->data['Module'][$field];
			}
		}
		//prd($this->data);
	}
	
	function admin_getParams($module)
	{
		Configure::write('debug', 0);
		$moduleObj = $this->Module->getModuleObject($module);
		$this->set(compact('moduleObj')); 
	}
	
	function admin_toggleEditorMode()
	{
		$sessionkey = "Module.moduleEditor";
		$enabled = $this->Session->read($sessionkey);
		$value = true;
		if($enabled)
		{
			$value = false;
		}
		$this->Session->write($sessionkey,$value);
		$msg = __("Module editor has been enabled.");
		if($value==false)
		{
			$msg = __("Module editor has been disabled.");
		}
		$this->successMsg($msg);
		if($value == true)
		{
			$this->redirect('/');
		}	
		$this->redirect($this->referer());
	}
	
	protected function  _save($data)
	{
		if(isset($data['Module']['name']))
		{
			$this->Module->setModule($data['Module']['name']);
		}
		$this->Module->create();
		$saved = $this->Module->saveAll($data);
		//exit;
		$this->set('saved',$saved);
		return $saved;
	}
	
	function admin_changeStatus($id = null)
	{
		$moduleData = $this->Module->findById($id);
		if(!$id || empty($moduleData))
		{
			if($this->RequestHandler->isAjax())
			{
				$this->_stop();
			} else {
				$this->errorMsg(__('Invalid URL!'));
				$this->redirect($this->referer());
			}
		}
		if($moduleData['Module']['status'] == Module::PUBLISHED)
		{
			$statusToSave = Module::UNPUBLISHED;
			$successMessage =  __('Module has been unpublished.');
			$errorMessage =  __('Module could not be unpublished.');
		} else {
			$statusToSave = Module::PUBLISHED;
			$successMessage =  __('Module has been published.');
			$errorMessage = __('Module could not be published.');
		}
		$this->Module->id = $id;
		if($this->Module->saveField('status',$statusToSave))
		{
			$this->successMsg($successMessage);
		} else {
			$this->errorMsg($errorMessage);
		}
		$this->redirect($this->referer());
	}
	
}
?>
